//
//  LoginViewController.swift
//  E-CycleForShop
//
//  Created by enghou on 17/3/1.
//  Copyright © 2017年 xyxorigation. All rights reserved.
//

import UIKit

class LoginViewController: BaseViewController {

    let accountField = InputView.init(frame: CGRect.init(x:56*YXWidthScale , y: 406*YXHeightScale, width: YXDeviceWidth - 112 * YXWidthScale, height: 88*YXHeightScale))
    
    let passField = InputView.init(frame: CGRect.init(x: 56*YXWidthScale, y: 526 * YXHeightScale, width: YXDeviceWidth - 112 * YXWidthScale, height: 88*YXHeightScale))
    var remem : Bool = false //提示是否记住密码
    let rememberPass = UIImageView.init()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.naviHide = true
        self.title = "登录"
        self.configView()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    @objc func forgetPas()->Void{
        let telphone = URL.init(string: "tel://0571-89939958")!
        UIApplication.shared.openURL(telphone)
    }
    //passwd	String	"e10adc3949ba59abbe56e057f20f883e"
    
    @objc func login()->Void{
        //do Login
        let result = accountField.isPhoneNumber()
        if !result {
            self.commonAlertMsg(content: YXPhoneWrong)
            return
        }
        
        let resultPass = passField.isPass()
        
        if !resultPass {
            self.commonAlertMsg(content: YXPassWrong)
            return
        }
        if result {
            let tel = accountField.content()
            let passwd = passField.content().md5()
            let param = ["account":tel,"password":passwd,"signInType":"1"]
            weak var sslf = self
            sslf?.beginAnim()
            Manager.shared().userLogin(param: param, success: { (model) in
                sslf?.endAnim()
                let mod = model as! LoginModel
                if mod.result {
                    //将一些信息存储起来
                    mod.saveToLocal()
                    if self.remem == true {
                        DataCacher.init().fillUName(name: tel)
                        DataCacher.init().fillPass(pass: self.passField.content())
                    } else {
                        DataCacher.init().fillUName(name: "")
                        DataCacher.init().fillPass(pass: "")
                    }
                    DataCacher.init().fillRemberPass(remember: self.remem)
                    NotificationCenter.default.post(name: NSNotification.Name("reset"), object: nil)
                    sslf?.dismiss(animated: true, completion: {
                    })
                }else{
                    sslf?.commonAlertMsg(content: mod.msg)
                }
            }, fail: { (code) in
                sslf?.endAnim()
                sslf?.commonAlertMsg(content: YXNetWorkFail)
            })
        } else {
        
        }
    }
    
    @objc func remember()->Void{
        remem = !remem
        if remem == false {
            rememberPass.image = nil
        }else{
            let img = UIImage.load(name: "right")
            rememberPass.image = img
        }
    }
    
    func configView() -> Void {
        self.view.backgroundColor = UIColor.white
        let imageView = UIImageView.init(frame: CGRect.init(x: (YXDeviceWidth-300*YXWidthScale)/2, y: 60*YXHeightScale, width: 300*YXWidthScale, height: 300*YXHeightScale))
        let image = UIImage.load(name: "icon_withdrawals")
        imageView.image = image
        
        self.view.addSubview(imageView)
        accountField.title = "账号"
        accountField.placeHolder = "请输入您的帐号"
        passField.title = "密码"
        passField.contentField.isSecureTextEntry = true
        passField.placeHolder = "请输入您的密码"
        self.view.addSubview(accountField)
        self.view.addSubview(passField)
        let loginAction = UIButton.init(type: UIButtonType.system)
        loginAction.setTitleColor(UIColor.white, for: .normal)
        loginAction.setTitle("登录", for: .normal)
        loginAction.titleLabel?.font = UIFont.systemFont(ofSize: 36*YXWidthScale)
        loginAction.layer.cornerRadius = YXCornerRadius
        loginAction.backgroundColor = UIColor.colorWithHexString(hex: "2c2c2c")
        loginAction.frame = CGRect.init(x: passField.left(), y: passField.bottom() + 40 * YXHeightScale, width: passField.width(), height: 88*YXHeightScale)
        loginAction.addTarget(self, action: #selector(login), for: .touchUpInside)
        self.view.addSubview(loginAction)
        
        let dataCach = DataCacher.init()
        remem = dataCach.rememberPass()
        
        rememberPass.layer.borderWidth = YXHeightScale
        rememberPass.layer.borderColor = UIColor.lightGray.cgColor
        rememberPass.frame = CGRect.init(x: passField.left(), y: loginAction.bottom()+40*YXHeightScale, width: 20 * YXWidthScale, height: 20 * YXHeightScale)
        if remem == true {
            passField.contentField.text = dataCach.pass()
            accountField.contentField.text = dataCach.uname()
            let img = UIImage.load(name: "right")
            rememberPass.image = img
        }
        self.view.addSubview(rememberPass)
        
        let rememberBtn = UIButton.init(type: .system)
        rememberBtn.frame = CGRect.init(x: rememberPass.right(), y: rememberPass.top(), width: 120 * YXWidthScale, height: 20 * YXHeightScale)
        rememberBtn.setTitle("记住密码", for: .normal)
        rememberBtn.setTitleColor(UIColor.lightGray, for: .normal)
        rememberBtn.titleLabel?.font = UIFont.systemFont(ofSize: 26 * YXWidthScale)
        rememberBtn.addTarget(self, action: #selector(remember), for: .touchUpInside)
        self.view.addSubview(rememberBtn)
        
        let forgetPass = UIButton.init(type: .system)
        let title = NSMutableAttributedString.init(string: "忘记密码")
        
        title.addAttributes([NSAttributedStringKey.foregroundColor:UIColor.colorWithHexString(hex: "2c2c2c"),NSAttributedStringKey.font : UIFont.systemFont(ofSize: 28*YXWidthScale)], range: NSRange.init(location: 0, length: 4))
        title.addAttribute(NSAttributedStringKey.underlineStyle, value: NSNumber.init(value: NSUnderlineStyle.styleSingle.rawValue), range: NSRange.init(location: 0, length: 4))
        forgetPass.setAttributedTitle(title, for: .normal)
        forgetPass.titleLabel?.font = UIFont.systemFont(ofSize: 28*YXWidthScale)
        forgetPass.setTitleColor(UIColor.colorWithHexString(hex: "2c2c2c"), for: .normal)
        let size = title.size()
        forgetPass.frame = CGRect.init(x: loginAction.right()-size.width, y: loginAction.bottom()+40*YXHeightScale, width: size.width, height: size.height)
        forgetPass.addTarget(self, action: #selector(forgetPas), for: .touchUpInside)
        self.view.addSubview(forgetPass)
        
        let size1 = "Copyright © 2017 All Rights Reserved".StringSize(font: UIFont.systemFont(ofSize: 20*YXWidthScale))
        let bottomLbl = UILabel.init(frame: CGRect.init(x: (YXDeviceWidth-size1.width)/2, y: YXDeviceHeight-38*YXHeightScale-size1.height, width: size1.width, height: size1.height))
        bottomLbl.text = "Copyright © 2017 All Rights Reserved"
        bottomLbl.textColor = UIColor.colorWithHexString(hex: "909090")
        bottomLbl.font = UIFont.systemFont(ofSize: 20*YXWidthScale)
        self.view.addSubview(bottomLbl)
    }

}
